# This test script performs the fourth of the benchmarks presented in
# the paper I am submitting to mobisys '10. 

# This benchmark is intended to show off the problems with using only 
# a task centric profile. A brightness and a sharpen task is performed
# with only onkelmac and halfnhalf online. Onkelmac should be best at 
# performing brightness and halfnhalf should be best at performing 
# the sharpen task. Using the task centric profile both tasks will be
# shipped to the strongest surrogate, which is halfnhalf. Using the 
# combined (adaptive) scheduler the correct distribution should be
# chosen.
#
# The input images used in the paper can be found in testdata/percom.

from __future__ import with_statement
import sys
import scavenger
from time import sleep, time

# Check command line args.
if len(sys.argv) < 4 or not '-s' in sys.argv:
    print 'Usage: mobisys4.py imagefile -s scheduler [-i iterations]'
    sys.exit(1)

# Read in the image file.
with open(sys.argv[1], 'rb') as infile:
    image = infile.read()

# Find out which scheduler to use.
scheduler = sys.argv[sys.argv.index('-s') + 1]
if not scheduler in ('tc', 'adaptive'):
    print 'Invalid scheduler. Valid values are tc and adaptive.'
    sys.exit(1)

# Check whether a number of test iterations is given.
iterations = 1
if '-i' in sys.argv:
    iterations = int(sys.argv[sys.argv.index('-i') + 1])

# Sleep for a little while to make sure that we discover
# the available surrogates.
sleep(2.0)

@scavenger.gprofilescavenge('len(#0)')
def gbrightness(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Brightness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.gprofilescavenge('len(#0)')
def gsharpen(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    factor = 1.0 + float(factor)
    new_image = ImageEnhance.Sharpness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.aprofilescavenge('len(#0)', 'len(#0)')
def abrightness(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Brightness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

@scavenger.aprofilescavenge('len(#0)', 'len(#0)')
def asharpen(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    factor = 1.0 + float(factor)
    new_image = ImageEnhance.Sharpness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

functions = []
if scheduler == 'tc':
    functions = [gbrightness, gsharpen]
else:
    functions = [abrightness, asharpen]

# Run the test!
for x in range(0, iterations):
    start = time()
    try:
        for function in functions:
            function(image, 1.3)
    except:
        print 'error',
    stop = time()
    print stop - start

scavenger.shutdown()
